import 'package:geolocator/geolocator.dart';
import 'package:geocoding/geocoding.dart';

class LocationService {

  /// 检查并请求定位权限
  Future<bool> checkAndRequestPermission() async {
    try {
      // 检查定位服务是否开启
      bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
      if (!serviceEnabled) {
        return false;
      }

      // 检查定位权限
      LocationPermission permission = await Geolocator.checkPermission();
      if (permission == LocationPermission.denied) {
        permission = await Geolocator.requestPermission();
        if (permission == LocationPermission.denied) {
          return false;
        }
      }

      if (permission == LocationPermission.deniedForever) {
        return false;
      }

      return true;
    } catch (e) {
      return false;
    }
  }

  /// 获取当前位置
  Future<Position?> getCurrentPosition() async {
    try {
      bool hasPermission = await checkAndRequestPermission();
      if (!hasPermission) {
        return null;
      }

      Position position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high,
        timeLimit: const Duration(seconds: 10),
      );

      return position;
    } catch (e) {
      return null;
    }
  }

  /// 根据经纬度获取地址信息
  Future<String> getAddressFromCoordinates(double latitude, double longitude) async {
    try {
      List<Placemark> placemarks = await placemarkFromCoordinates(latitude, longitude);
      
      if (placemarks.isNotEmpty) {
        Placemark place = placemarks[0];
        String address = '';
        
        if (place.administrativeArea != null && place.administrativeArea!.isNotEmpty) {
          address += place.administrativeArea!;
        }
        
        if (place.locality != null && place.locality!.isNotEmpty) {
          if (address.isNotEmpty) address += '';
          address += place.locality!;
        }
        
        if (place.subLocality != null && place.subLocality!.isNotEmpty) {
          if (address.isNotEmpty) address += '';
          address += place.subLocality!;
        }
        
        if (address.isEmpty) {
          address = '未知位置';
        }
        
        return address;
      }
      
      return '未知位置';
    } catch (e) {
      return '位置获取失败';
    }
  }

  /// 获取当前位置和地址
  Future<Map<String, dynamic>> getCurrentLocationInfo() async {
    try {
      Position? position = await getCurrentPosition();
      if (position == null) {
        return {
          'success': false,
          'address': '定位失败',
          'latitude': null,
          'longitude': null,
        };
      }

      String address = await getAddressFromCoordinates(
        position.latitude, 
        position.longitude
      );

      return {
        'success': true,
        'address': address,
        'latitude': position.latitude,
        'longitude': position.longitude,
      };
    } catch (e) {
      return {
        'success': false,
        'address': '定位失败',
        'latitude': null,
        'longitude': null,
      };
    }
  }
}